package com.wwmz.pattern.singleton.destroy;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * Created by wumz on 2020/9/17 10:15
 */
public class SerializeSingletonTest {

    public static void main(String[] args) {
        //把singleton2值赋给singleton1
        SeriableSingleton singleton1 = null;
        SeriableSingleton singleton2 = SeriableSingleton.getInstance();
        try {
            FileOutputStream out = new FileOutputStream("SeriableSingleton.obj");
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(out);
            objectOutputStream.writeObject(singleton2);
            objectOutputStream.flush();
            objectOutputStream.close();

            FileInputStream in = new FileInputStream("SeriableSingleton.obj");
            ObjectInputStream objectInputStream = new ObjectInputStream(in);
            singleton1 = (SeriableSingleton)objectInputStream.readObject();
            objectInputStream.close();

            System.out.println(singleton1);
            System.out.println(singleton2);
            System.out.println(singleton2 == singleton1);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
